from sqlalchemy.ext.asyncio import AsyncSession

from exceptions.exception import ServiceException
from module_admin.entity.vo.common_vo import CrudResponseModel
from module_stock.dao.account_dao import AccountDao
from module_stock.entity.vo.account_vo import DeleteAccountModel, AccountModel, AccountPageQueryModel
from utils.common_util import CamelCaseUtil


class AccountService:
    """
    股票账号模块服务层
    """

    @classmethod
    async def get_account_list_services(
            cls, query_db: AsyncSession, query_object: AccountPageQueryModel, is_page: bool = False
    ):
        """
        获取股票账号列表信息service

        :param query_db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 股票账号列表信息对象
        """
        account_list_result = await AccountDao.get_account_list(query_db, query_object, is_page)

        return account_list_result

    @classmethod
    async def add_account_services(cls, query_db: AsyncSession, page_object: AccountModel):
        """
        新增股票账号信息service

        :param query_db: orm对象
        :param page_object: 新增股票账号对象
        :return: 新增股票账号校验结果
        """
        try:
            await AccountDao.add_account_dao(query_db, page_object)
            await query_db.commit()
            return CrudResponseModel(is_success=True, message='新增成功')
        except Exception as e:
            await query_db.rollback()
            raise e

    @classmethod
    async def edit_account_services(cls, query_db: AsyncSession, page_object: AccountModel):
        """
        编辑股票账号信息service

        :param query_db: orm对象
        :param page_object: 编辑股票账号对象
        :return: 编辑股票账号校验结果
        """
        edit_account = page_object.model_dump(exclude_unset=True,
                                              exclude={'account_no', 'del_flag', 'create_by', 'create_time', })
        account_info = await cls.account_detail_services(query_db, page_object.id)
        if account_info.id:
            try:
                await AccountDao.edit_account_dao(query_db, edit_account)
                await query_db.commit()
                return CrudResponseModel(is_success=True, message='更新成功')
            except Exception as e:
                await query_db.rollback()
                raise e
        else:
            raise ServiceException(message='股票账号不存在')

    @classmethod
    async def delete_account_services(cls, query_db: AsyncSession, page_object: DeleteAccountModel):
        """
        删除股票账号信息service

        :param query_db: orm对象
        :param page_object: 删除股票账号对象
        :return: 删除股票账号校验结果
        """
        if page_object.ids:
            id_list = page_object.ids.split(',')
            try:
                for id in id_list:
                    await AccountDao.delete_account_dao(query_db, AccountModel(id=id))
                await query_db.commit()
                return CrudResponseModel(is_success=True, message='删除成功')
            except Exception as e:
                await query_db.rollback()
                raise e
        else:
            raise ServiceException(message='传入主键ID为空')

    @classmethod
    async def account_detail_services(cls, query_db: AsyncSession, id: int):
        """
        获取股票账号详细信息service

        :param query_db: orm对象
        :param id: 主键ID
        :return: 主键ID对应的信息
        """
        account = await AccountDao.get_account_detail_by_id(query_db, id=id)
        if account:
            result = AccountModel(**CamelCaseUtil.transform_result(account))
        else:
            result = AccountModel(**dict())

        return result

    @classmethod
    async def check_account_no_unique_services(cls, query_db: AsyncSession, account_no: str):
        """
        校验账号是否唯一service

        :param query_db: orm对象
        :param account_no: 账号
        :return: 校验结果
        """
        account = await AccountDao.get_account_detail_by_account_no(query_db, account_no=account_no)
        if account:
            return False  # 账号已存在
        return True  # 账号不存在，可以使用
